﻿using MiniFox.Resources;
using MiniFox.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Core.Models.VO
{
    public class SequenceSetVo
    {
        [Key]
        [Column("name")]
        public string Name { get; set; }

        [Display(Name = "前缀", ShortName = "前缀")]
        [Column("prefix")]
        public string Prefix { get; set; }

        [Display(Name = "后缀", ShortName = "后缀")]
        [Column("suffix")]
        public string? Suffix { get; set; }

        [Display(Name = "匹配格式", ShortName = "匹配格式")]
        [Column("pattern")]
        public string Pattern { get; set; }

        [Display(Name = "是否基于UTC时间", ShortName = "是否基于UTC时间")]
        [Column("baseOnUtc")]
        public bool BaseOnUtc { get; set; } = false;

        [JsonIgnore]
        public int CurrentVal { get; set; }

        [Display(Name = "当前序列值", ShortName = "当前序列值")]
        [Column("val")]
        public string Val
        {
            get
            {
                return this.CurrentVal.ToString($"D{this.Length}");
            }
        }

        [Column("nextVal")]
        public string NextVal
        {
            get
            {
                int val = 0;
                if (CurrentVal < this.MaxVal)
                    val = CurrentVal;
                return (++val).ToString($"D{this.Length}");
            }
        }

        [Display(Name = "序列最大值", ShortName = "序列最大值")]
        [Column("maxVal")]
        public int MaxVal { get; set; }

        [Display(Name = "当前ID", ShortName = "当前ID")]
        public string CurrentId 
        {
            get
            {
                var now = this.BaseOnUtc ? DateTime.UtcNow : DateTime.Now;
                return $"{this.Prefix}{now.DateTimeFormat(this.Pattern)}{this.Val}{this.Suffix}";
            }
        }

        public string NextId
        {
            get
            {
                var now = this.BaseOnUtc ? DateTime.UtcNow : DateTime.Now;
                return $"{this.Prefix}{now.DateTimeFormat(this.Pattern)}{this.NextVal}{this.Suffix}";
            }
        }

        [Display(Name = "后缀长度", ShortName = "后缀长度")]
        [Column("length")]
        public byte Length { get; set; }

        [Display(Name = "序列描述", ShortName = "序列描述")]
        [Column("description")]
        public string? Description { get; set; }

        [Display(Name = "修改时间", ShortName = "修改时间")]
        [Column("updateTime")]
        public DateTime UpdateTime { get; set; }
    }
}
